$().ready(function(){

$("#ManualAddBook #alternative-element").hide();

var baseUrl = $("#ManualAddBook #baseurl").val();

$("#ManualAddBook #Continue").live("click", function() {
	$("#ManualAddBook #ISBN").attr("disabled", true);
	$.ajax({
        url: baseUrl + '/book/fetchisbn',
        type: 'post',
        dataType: 'json',
        async: true,
        data: ({'isbn' : $("#ISBN").val()}),
        success: function(data) {
		    // exist book, show add book option directly
			if(data["existBook"]) {
				$("#ManualAddBook #name").attr("readonly", "readonly");
				$("#ManualAddBook #description").attr("readonly", "readonly");
				$("#ManualAddBook #detailPageUrl").attr("readonly", "readonly");
				$("#ManualAddBook #image").attr("readonly", "readonly");
				
				$("#ManualAddBook #name").val(data["book"]["name"]);
				$("#ManualAddBook #description").val(data["book"]["description"]);
				$("#ManualAddBook #detailPageUrl").val(data["book"]["detailPageUrl"]);
				$("#ManualAddBook #image").val(data["book"]["image"]);
				
				//$("#ManualAddBook #alternative-element").show();
				window.location = baseUrl + "/book/add/id/" + data["book"]["ISBN"];
			} else {
		    // new book, show continue form of add book
				$("#ManualAddBook #name").removeAttr("readonly");
				$("#ManualAddBook #description").removeAttr("readonly");
				$("#ManualAddBook #detailPageUrl").removeAttr("readonly");
				$("#ManualAddBook #image").removeAttr("readonly");
				
				$("#ManualAddBook #name").val("");
				$("#ManualAddBook #description").val("");
				$("#ManualAddBook #detailPageUrl").val("");
				$("#ManualAddBook #image").val("");
				$("#ManualAddBook #alternative-element").show();
				
			}
        }});
	
});
});